<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/29
 * Time: 14:18
 */

namespace osc\admin\controller;

use osc\common\controller\AdminBase;
use think\Db;
use think\Request;

class Contract extends AdminBase
{
    protected function _initialize(){
        parent::_initialize();
        $this->assign('breadcrumb1','合约');
        $this->assign('breadcrumb2','合约列表');
        $this->assign('breadcrumb3','合约周期');
        $this->assign('breadcrumb4','合约金额');
        $this->assign('breadcrumb5','合约利率');
        $this->assign('breadcrumb6','品种价格');
        $this->assign('breadcrumb7','合约开放名单');
    }

    /**
     * 合约列表
     * @return mixed
     */
    public function index(){
        $map = [];
        $name = $this->request->param('name');
        $create_time = $this->request->param('create_time');
        if(!empty($name)){
            $map['name'] = ['like','%'.$name.'%'];
        }
        if(!empty($create_time)){
            $map["DATE_FORMAT(FROM_UNIXTIME(create_time),'%Y-%m-%d')"] = $create_time;
        }
//        print_r($map);exit;
        $list = Db::name('contract_info')->where($map)->paginate(10,false, [
            'query' => Request::instance()->param(),//不丢失已存在的url参数
        ]);
        $page = $list->render();
        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }

    /**
     * 新增合约
     * @return mixed
     */
    public function add(){
        if(request()->isPost()){
            $data = $this->request->post();
            $data['create_time'] = time();
            $res = Db::name('contract_info')->insertGetId($data);
            if($res){
                storage_user_action(UID,admin('username'),config('BACKEND_USER'),'新增合约--'.$res);
                $this->success('新增成功！',url('Contract/index'));
            }else{
                $this->error('新增失败！');
            }
        }
        $this->assign('crumbs', '新增');
        return $this->fetch();
    }

    /**
     * 修改合约
     * @return mixed
     */
    public function update(){
        if(request()->isPost()){
            $data = $this->request->post();
            $res = Db::name('contract_info')->where('id',$data['id'])->update($data);
            if($res){
                storage_user_action(UID,admin('username'),config('BACKEND_USER'),'修改了合约-'.$res);
                $this->success('修改成功！',url('Contract/index'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $data = Db::name('contract_info')->where('id',input('id'))->find();
            $this->assign('info',$data);
        }
        $this->assign('crumbs', '修改');
        return $this->fetch();
    }
    /**
     * 删除合约
     */
    public function del(){
        $param = $this->request->param();

        $res = Db::name($param['model'])->where('id',$param['id'])->delete();
        if($res){
            storage_user_action(UID,admin('username'),config('BACKEND_USER'),'删除了合约配置-'.$param['id']);
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }

    /**
     * 周期列表
     */
    public function time_config(){
        $param = $this->request->param();
        $map = [];
        if(!empty($param['num'])){
            $map['num'] = $param['num'];
        }
        if(!empty($param['type'])){
            $map['type'] = $param['type'];
        }
        $list = Db::name('contract_time_config')->where($map)->paginate(10,false, [
            'query' => Request::instance()->param(),//不丢失已存在的url参数
        ]);
        $page = $list->render();
        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }

    /**
     * 新增合约周期配置
     * @return mixed
     */
    public function time_add(){
        if(request()->isPost()){
            $data = $this->request->post();
            $res = Db::name('contract_time_config')->insertGetId($data);
            if($res){
                storage_user_action(UID,admin('username'),config('BACKEND_USER'),'新增合约周期--'.$res);
                $this->success('新增成功！',url('Contract/time_config'));
            }else{
                $this->error('新增失败！');
            }
        }
        $this->assign('crumbs', '新增');
        return $this->fetch();
    }

    /**
     * 修改合约周期
     * @return mixed
     */
    public function time_update(){
        if(request()->isPost()){
            $data = $this->request->post();
            $res = Db::name('contract_time_config')->where('id',$data['id'])->update($data);
            if($res){
                storage_user_action(UID,admin('username'),config('BACKEND_USER'),'修改了合约周期配置--'.$data['id']);
                $this->success('修改成功！',url('Contract/time_config'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $data = Db::name('contract_time_config')->where('id',input('id'))->find();
            $this->assign('info',$data);
        }
        $this->assign('crumbs', '修改');
        return $this->fetch();
    }


    /**
     * 合约价格
     */
    public function money_config(){
        $param = $this->request->param();
        $map = [];
        if(!empty($param['min_price'])){
            $map['min_price'] = ['>',$param['min_price']];
        }
        if(!empty($param['max_price'])){
            $map['max_price'] = ['<',$param['max_price']];
        }
        $list = Db::name('contract_money_config')
            ->where($map)
            ->paginate(10,false, [
            'query' => Request::instance()->param(),//不丢失已存在的url参数
        ]);
        $page = $list->render();
        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }

    /**
     * 新增合约价格配置
     * @return mixed
     */
    public function money_add(){
        if(request()->isPost()){
            $data = $this->request->post();
            $res = Db::name('contract_money_config')->insertGetId($data);
            if($res){
                storage_user_action(UID,admin('username'),config('BACKEND_USER'),'新增合约价格配置--'.$res);
                $this->success('新增成功！',url('Contract/money_config'));
            }else{
                $this->error('新增失败！');
            }
        }
        $this->assign('crumbs', '新增');
        return $this->fetch();
    }

    /**
     * 修改金额配置
     * @return mixed
     */
    public function money_update(){
        if(request()->isPost()){
            $data = $this->request->post();
            $res = Db::name('contract_money_config')->where('id',$data['id'])->update($data);
            if($res){
                storage_user_action(UID,admin('username'),config('BACKEND_USER'),'修改了合约价格配置--'.$data['id']);
                $this->success('修改成功！',url('Contract/time_config'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $data = Db::name('contract_money_config')->where('id',input('id'))->find();
            $this->assign('info',$data);
        }
        $this->assign('crumbs', '修改');
        return $this->fetch();
    }

    /**
     * 合约利率
     */
    public function rate_config(){
        $param = $this->request->param();
        $map = [];
        if(!empty($param['info_id'])){
            $map['b.id'] = $param['info_id'];
        }
        if(!empty($param['time_id'])){
            $map['c.id'] = $param['time_id'];
        }
        if(!empty($param['money_id'])){
            $map['d.id'] = $param['money_id'];
        }
        $list = Db::name('contract_rate_config a,osc_contract_info b,osc_contract_time_config c,osc_contract_money_config d')
            ->where($map)
            ->where('a.contract_id=b.id and a.time_id=c.id and a.money_id=d.id')
            ->field('a.id,a.rate,a.remark,b.name,c.num,c.type,d.min_price,d.max_price,a.state')
			->order('a.state desc')
            ->order('a.contract_id asc,d.min_price,c.type')
            ->paginate(10,false, [
            'query' => Request::instance()->param(),//不丢失已存在的url参数
        ]);
        $page = $list->render();
		//查询合约
        $info = Db::name('contract_info')->select();
        //查询时间
        $time = Db::name('contract_time_config')->select();
        //查询价格
        $money = Db::name('contract_money_config')->select();

        $this->assign('money',$money);
        $this->assign('time',$time);
        $this->assign('info',$info);
        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }

    /**
     * 新增合约利率
     * @return mixed
     */
    public function rate_add(){
        if(request()->isPost()){
            $data = $this->request->post();

            $rate = Db::name('contract_rate_config')->where(['contract_id'=>$data['contract_id'],'money_id'=>$data['money_id'],'time_id'=>$data['time_id']])->find();
            if($rate){ $this->error('当前区间已被使用'); }

            $res = Db::name('contract_rate_config')->insertGetId($data);
            if($res){
                storage_user_action(UID,admin('username'),config('BACKEND_USER'),'新增合约价格配置--'.$res);
                $this->success('新增成功！',url('Contract/rate_config'));
            }else{
                $this->error('新增失败！');
            }
        }
        $money = Db::name('contract_money_config')->select();
        $contract_info = Db::name('contract_info')->select();
        $time_info = Db::name('contract_time_config')->select();
        $this->assign('contract',$contract_info);
        $this->assign('time_info',$time_info);
        $this->assign('money',$money);
        $this->assign('crumbs', '新增');
        return $this->fetch();
    }

    /**
     * 修改利率配置
     * @return mixed
     */
    public function rate_update(){
        if(request()->isPost()){
            $data = $this->request->post();
            $res = Db::name('contract_rate_config')->where('id',$data['id'])->update($data);
            if($res){
                storage_user_action(UID,admin('username'),config('BACKEND_USER'),'修改了合约价格配置--'.$data['id']);
                $this->success('修改成功！',url('Contract/rate_config'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $data = Db::name('contract_rate_config')->where('id',input('id'))->find();
            $contract_info = Db::name('contract_info')->select();
            $time_info = Db::name('contract_time_config')->select();
            $money = Db::name('contract_money_config')->select();
            $this->assign('contract',$contract_info);
            $this->assign('time_info',$time_info);
            $this->assign('money',$money);
            $this->assign('info',$data);
        }
        $this->assign('crumbs', '修改');
        return $this->fetch();
    }

    /**
     * 品种价格配置
     */
    public function issue_config(){
        $param = $this->request->param();
        $map = [];
        if(!empty($param['exchange'])){
            $map['b.id'] = $param['exchange'];
        }
        if(!empty($param['variety'])){
            $map['c.id'] = $param['variety'];
        }
        $list = Db::name('contract_issue a,osc_exchange_house b,osc_exchage_variety c')
            ->where($map)
            ->where('a.exchange_id=b.id and a.variety_id=c.id')
            ->field('a.id,a.issue,a.target,a.remark,b.name exchange,c.name variety')
            ->paginate(10,false, [
                'query' => Request::instance()->param(),//不丢失已存在的url参数
            ]);
        $page = $list->render();
        $exchange = Db::name('exchange_house')->where('state',1)->select();
        $variety = Db::name('exchage_variety')->where('state',1)->select();
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('exchange',$exchange);
        $this->assign('variety',$variety);
        return $this->fetch();
    }


    /**
     * 新增品种价格
     * @return mixed
     */
    public function issue_add(){
        if(request()->isPost()){
            $data = $this->request->post();
            $res = Db::name('contract_issue')->insertGetId($data);
            if($res){
                storage_user_action(UID,admin('username'),config('BACKEND_USER'),'新增品种价格--'.$res);
                $this->success('新增成功！',url('Contract/issue_config'));
            }else{
                $this->error('新增失败！');
            }
        }
        $exchange = Db::name('exchange_house')->where('state',1)->select();
        $variety = Db::name('exchage_variety')->where('state',1)->select();
        $this->assign('exchange',$exchange);
        $this->assign('variety',$variety);
        $this->assign('crumbs', '新增');
        return $this->fetch();
    }

    /**
     * 修改品种价格
     * @return mixed
     */
    public function issue_update(){
        if(request()->isPost()){
            $data = $this->request->post();
            $res = Db::name('contract_issue')->where('id',$data['id'])->update($data);
            if($res){
                storage_user_action(UID,admin('username'),config('BACKEND_USER'),'修改了品种价格--'.$data['id']);
                $this->success('修改成功！',url('Contract/issue_config'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $data = Db::name('contract_issue')->where('id',input('id'))->find();
            $exchange = Db::name('exchange_house')->where('state',1)->select();
            $variety = Db::name('exchage_variety')->where('state',1)->select();
            $this->assign('exchange',$exchange);
            $this->assign('variety',$variety);
            $this->assign('info',$data);
        }
        $this->assign('crumbs', '修改');
        return $this->fetch();
    }

    /**
     * 修改价格区间是否显示
     */
    public function display_state(){
        $data = $this->request->param();
        if($data){
            $res = Db::name('contract_money_config')->where('id',$data['id'])->update(['display'=>$data['status']]);
            if($res){
                $this->redirect('Contract/money_config');
            }else{
                $this->error('操作失败');
            }
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * 合约开放名单
     * @return mixed
     */
    public function open(){
        $data = $this->request->param();
        $map = [];
        if(!empty($data['name'])){
            $map['username'] = ['like','%'.$data['name'].'%'];
        }
        if(!empty($data['create_time'])){
            $map["DATE_FORMAT(FROM_UNIXTIME(create_time),'%Y-%m-%d')"] = $data['create_time'];
        }
        $list = Db::name('contract_open')
            ->where($map)
            ->paginate(10,false, [
                'query' => Request::instance()->param(),//不丢失已存在的url参数
            ]);
        $page = $list->render();
        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }

    /**
     * 新增合约开放名单
     * @return array
     */
    public function record(){
        if($this->request->isPost()){
            $data = $this->request->param();

            $user = Db::name('member')->where('username',$data['username'])->find();
            if(!$user){ return ['code'=>0,'msg'=>'用户不存在']; }

            $open = Db::name('contract_open')->where('username',$data['username'])->find();
            if($open){ return ['code'=>0,'msg'=>'已添加']; }

            $data['create_time'] = time();
            $res = Db::name('contract_open')->insert($data);

            if($res){
                return ['code'=>1,'msg'=>'新增成功'];
            }
            return ['code'=>0,'msg'=>'新增失败'];
        }else{
            return ['code'=>0,'msg'=>'提交错误'];
        }
    }
	
	/**
     * 利率状态修改
     * @throws \think\Exception
     */
    public function rate_state(){
        $param = $this->request->param();

        if(!$param){
            $this->error('操作失败');
        }

        $res = Db::name('contract_rate_config')->where('id',$param['id'])->update(['state'=>$param['status']]);
        if($res){
            $this->redirect('contract/rate_config');
        }else{
            $this->error('修改失败');
        }
    }
	
	/**
     * 限制合约申请设置列表
     * @return mixed
     */
    public function limit(){
        $list = Db::name('limit')->paginate(10,false, [
            'query' => Request::instance()->param(),//不丢失已存在的url参数
        ]);
        $page = $list->render();

        $time = Db::name('contract_time_config')->select();

        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('time',$time);
        $this->assign('breadcrumb2','限制合约申请设置');
        return $this->fetch();
    }

    /**
     * 新增合约限制规则
     */
    public function limit_add(){
        if($this->request->isPost()){
            $param = $this->request->param();

            if($param['time_id'] == null or $param['num'] == null or $param['time_id'] == '' or $param['num'] == ''){

                $this->error('关联值和限制值不能为空');
            }

            $limit = Db::name('limit')->where('time_id',$param['time_id'])->find();

            if($limit){
                $rew = Db::name('limit')->where('time_id',$param['time_id'])->update(['num'=>$param['num'],'desc'=>$param['desc']]);

                if($rew){
                    $this->success('修改成功');
                }else{
                    $this->error('修改失败');
                }
            }

            $res = Db::name('limit')->insert($param);

            if($res){
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }

        $this->error('提交错误');
    }
}